package cn.bupt.adt.controller.inf;

import cn.bupt.adt.service.IDoctorAppUserService;
import cn.bupt.adt.service.IOrderService;
import cn.bupt.adt.util.NetDataAccessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/interface/doctor/order")
public class DoctorOrderController {

    @Autowired
    IOrderService orderService;

    @Autowired
    IDoctorAppUserService doctorAppUserService;

    // 医生获取未接订单，包括（status=0）or（status=5&&cancelBy=Constants.CANCEL_BY_DOC）
    @RequestMapping(value = "/getOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getOrders(HttpServletRequest request, int pageNum, int pageSize) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        Map<String, Object> content = orderService.getCanTakedOrdersByDoctorId(userId, pageNum, pageSize, request);
        if (content.isEmpty()) {
            nau.setContent(null);
            nau.setResultDesp("该医生没有手术标签，无法接任何手术，请与管理员联系解决");
            nau.setResult(0);
        } else {
            nau.setContent(content);
            nau.setResultDesp("获取订单成功");
            nau.setResult(1);
        }
        return nau;
    }

    @RequestMapping(value = "/getUndoOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getUndoOrders(HttpServletRequest request, int pageNum, int pageSize) throws IllegalAccessException, IOException {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        Map<String, Object> content = orderService.getUndoOrdersByDoctorId(userId, pageNum, pageSize, request);
        nau.setContent(content);
        nau.setResultDesp("获取已接单未开始手术未完成订单成功");
        nau.setResult(1);
        return nau;
    }

    @RequestMapping(value = "/getUnfinishOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getUnfinishOrders(HttpServletRequest request, int pageNum, int pageSize) throws IllegalAccessException, IOException {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        Map<String, Object> content = orderService.getUnfinishOrdersByDoctorId(userId, pageNum, pageSize, request);
        nau.setContent(content);
        nau.setResultDesp("获取已接单开始手术未完成订单成功");
        nau.setResult(1);
        return nau;
    }

    /**
     * 获取已完成的订单
     */
    @RequestMapping(value = "/getDoneOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getDoneOrders(HttpServletRequest request, int pageNum, int pageSize, Integer userId) throws IllegalAccessException, IOException {
        Map<String, Object> content = orderService.getDoneOrdersByDoctorId(userId, pageNum, pageSize, request);
        return NetDataAccessUtil.success().resultDesp("获取完成订单成功").content(content);
    }

    /**
     * 获取进行中的订单
     */
    @RequestMapping(value = "/getOnGoingOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getOnGoing(HttpServletRequest request, int pageNum, int pageSize, Integer userId) throws IllegalAccessException, IOException {
        Map<String, Object> content = orderService.getOnGoingOrdersOrdersByDoctorId(userId, pageNum, pageSize, request);
        return NetDataAccessUtil.success().resultDesp("获取进行中订单成功").content(content);
    }

    // 医生获取完成订单（彻底完成，双方都已确认）， 包括status为1,2,3
    @RequestMapping(value = "/getFinishedOrders", method = RequestMethod.POST)
    @ResponseBody
    public Object getFinishedOrders(HttpServletRequest request, int pageNum, int pageSize) throws IllegalAccessException, IOException {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        Map<String, Object> content = orderService.getFinishedOrdersByDoctorId(userId, pageNum, pageSize, request);
        nau.setContent(content);
        nau.setResultDesp("获取进行中订单成功");
        nau.setResult(1);
        return nau;
    }

    /**
     * 医生接单
     */
    @RequestMapping(value = "/takeOrder", method = RequestMethod.POST)
    @ResponseBody
    public synchronized Object takeOrder(int orderId, Integer userId) {
        if (orderService.takeOrder(orderId, userId)) {
            return NetDataAccessUtil.success().resultDesp("接单成功");
        }
        return NetDataAccessUtil.error().resultDesp("该手术已被其他医生接单,或您的余额不足或其他网络原因，接单失败");
    }
}
